package org.crazyit.booksys.action;

import java.util.ArrayList;
import java.util.List;

import org.crazyit.booksys.domain.Book;
import org.crazyit.booksys.domain.Order;
import org.crazyit.booksys.service.BookService;
import org.crazyit.booksys.service.OrderService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class OrderAction extends ActionSupport {
	
	private OrderService orderService;
	private BookService bookService;
	private List<Book> books;
	private List<String> bookIds;
	private List<Order> orders;
	private Integer userId;
	private Integer id;
	private Order order;
	private String orderState;
	private boolean[] buylist;
	private Integer[] ids;
	public List<Integer> seleteids(){
		List<Integer> selectedids = new ArrayList<Integer>();
		for(int i = 0;i<buylist.length;i++){
			System.out.println("你选择的书本id"+ids[i]);
			selectedids.add(ids[i]);
		}
		return selectedids;
		
	}
	//�µ�
	public String placeOrder(){
		List<Integer> selectedids = seleteids();
		System.out.print("");
		books=new ArrayList<Book>();
		for(int s:selectedids){
				Book book=bookService.findbyid(s);
				System.out.println("bookid为"+book.getId());
				books.add(book);
			
		}
		if(orderService.placeOrder(this.getUserId(), books)>0){
			return notPayOrders();
		}
		return INPUT;
	}
	
	//��ʾδ֧������
	public String notPayOrders(){
		System.out.print("notpagorder");
		setOrders(orderService.getNotPayOrders(this.getUserId()));
		return SUCCESS;
	}
	
	//֧������
	public String payOrder(){
		System.out.print("pay方法");
		setOrder(orderService.getOrder(getId()));
		orderService.payOrder(getOrder());
		return notTakeBookOrders();
	}
	
	//��ʾ���ջ��Ķ���
	public String notTakeBookOrders(){
		System.out.print("进入到notTakeBookOrders");
		setOrders(orderService.getAlreadyPayOrders(this.getUserId()));
		orders.addAll(orderService.getSendingOrders(this.getUserId()));
		
		return SUCCESS;
	}
	
	//ȷ���ջ�
	public String takeBook(){
		System.out.print("");
		setOrder(orderService.getOrder(getId()));
		orderService.takeBooks(getOrder());
		return endOrders();
	}
	
	//��ʾ�ѽ���Ķ���
	public String endOrders(){
		System.out.print("");
		setOrders(orderService.getEndOrders(this.getUserId()));
		return SUCCESS;
	}
	
	//��ʾȫ���Ķ���
	public String allOrders(){
		System.out.print("");
		setOrders(orderService.getUserAllOrders(this.getUserId()));
		return "listorder";
	}
	
	//��ʾδ����Ķ���
	public String untreatedOrders(){
		System.out.print("untreatedorder方法");
		setOrders(orderService.getAlreadyPayOrders());
		return SUCCESS;
	}
	//��ʾ������ϸ
	public String detail(){
		System.out.print("");
		setOrder(orderService.getOrder(getId()));
		setBooks(orderService.getOrderDetail(getOrder()));
		return "detail";
	}
	
	//���ܶ���
	public String acceptOrder(){
		System.out.print("");
		setOrder(orderService.getOrder(getId()));
		orderService.acceptOrder(getOrder());
		return processedOrders();
	}
	
	//��ʾ�Ѵ���Ķ���
	public String processedOrders(){
		System.out.print("");
		setOrders(orderService.getSendingOrders());
		return SUCCESS;
	}
	
	public String list()
	{
		setBooks(bookService.getAllBooksByPage(1,10));
		return SUCCESS;
	}
	
	public OrderService getOrderService() {
		return orderService;
	}
	public void setOrderService(OrderService orderService) {
		
		this.orderService = orderService;
	}
	public List<Book> getBooks() {
		return books;
	}
	public void setBooks(List<Book> books) {
		this.books = books;
	}
	public int getUserId() {
		ActionContext context = ActionContext.getContext();
		return (Integer)context.getSession().get("userid");
	}
	public void setUserId() {
		
	}
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}

	public void setOrderState(String orderState) {
		this.orderState = orderState;
	}

	public String getOrderState() {
		return orderState;
	}


	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}


	public BookService getBookService() {
		return bookService;
	}

	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}

	public List<Order> getOrders() {
		return orders;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return id;
	}

	public void setBookIds(List<String> bookIds) {
		this.bookIds = bookIds;
	}

	public List<String> getBookIds() {
		return bookIds;
	}
	public boolean[] getBuylist() {
		return buylist;
	}
	public void setBuylist(boolean[] buylist) {
		this.buylist = buylist;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	
	
}
